<?php
/**
 * @version		$Id: manager.php 10381 2008-06-01 03:35:53Z yopensource $
 * @package		YOS
 * @subpackage	Crawler
 * @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.application.component.model');

// Set the table directory
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');

/**
 * Crawler Component link Model
 *
 * @package		Joomla
 * @subpackage	Content
 * @since 1.5
 */
class FVBModelContents extends JModel
{
	/**
	 * amMap data array
	 *
	 * @var array
	 */
	var $_data = null;	

	/**
	 * uri total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;
	
	var $_contentEdit	=	null;
	/**
	 * Constructor
	 *
	 * @since 0.9
	 */
	function __construct()
	{
		parent::__construct();

		global $mainframe,$option;
		
		//$context			= 'com_content.viewcontent';
		$filter_order		= $mainframe->getUserStateFromRequest( $option.'.contents_feed.filter_order', 		'filter_order', 	'id',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'.contents_feed.filter_order_Dir',	'filter_order_Dir',	'',				'word' );	
		$search 			= $mainframe->getUserStateFromRequest( $option.'.contents_feed.search', 			'search', 			'',				'string' );
		$search 			= JString::strtolower( $search );

		$limit				= $mainframe->getUserStateFromRequest($option.'.contents_feed.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart			= $mainframe->getUserStateFromRequest($option.'.contents_feed.limitstart', 'limitstart', 0, 'int');
		
		$this->setState('limit', $limit);
		$this->setState('limitstart',$limitstart);
		$this->setState('search',$search);		

		// Get the filter request variables
		$this->setState('filter_order', 	$filter_order);
		$this->setState('filter_order_dir', $filter_order_Dir);
	}
	
	function getTotal()
	{
		// Lets load the total nr if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}
	
	function getPagination()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}
	
	
	function getData(){
		$pop	= JRequest::getBool('pop');
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			if ($pop) {
				$this->_data = $this->_getList( $query );
			} else {
				$this->_data = $this->_getList( $query, $this->getState('limitstart'), $this->getState('limit') );
			}
		}

		return $this->_data;
	}
	
	function _buildQuery(){
		// Get the WHERE and ORDER BY clauses for the query
		$where		= $this->_buildWhere();
		$orderby	= $this->_buildOrderBy();

		$config 	= &JComponentHelper::getParams('com_feed_manager');
		$data_type 	= $config->get('data_type');	
	
		//Get Events from Database
		$query = 'SELECT CR.*,C.date_original FROM #__fcontent_receive AS CR left join #__fcontent AS C ON CR.cid = C.id'							
				. $where
				. $orderby
				;	
		return $query;		
	}
	
	function _buildWhere( )
	{
		$db						=	& JFactory::getDBO();		
		$search					=	$this->getState('search');

		$where = array();
		if ( $search ) {
			$w 		= array();
			$w[] 	= 'CR.cid LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			$w[] 	= 'CR.title LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			$w[] 	= 'CR.SourceURL LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			$w 		= implode( ' OR ', $w );
			$where[]=	'('.$w.')';
		}
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );		
		return $where;
	}
	
	function _buildOrderBy()
	{
		$filter_order		=	$this->getState('filter_order');
		$filter_order_Dir	=	$this->getState('filter_order_dir');
		
		$orderby 	= ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', id';
		
		return $orderby;
	}
	
	function getLists(){
		
		// table ordering
		$lists['order_Dir']	= $this->getState('filter_order_dir');
		$lists['order']		= $this->getState('filter_order');
		
		// search filter
		$lists['search']= $this->getState('search');		
		
		$filter		= null;
		
		$config 	= &JComponentHelper::getParams('com_feed_manager');
		$data_type 	= $config->get('data_type');
		return $lists;
	}

	function remove()
	{
		// Initialize variables
		$db 		=& JFactory::getDBO();
		$cid 		= JRequest::getVar('cid', array(0), 'post', 'array');
		$nullDate	= $db->getNullDate();

		if (count($cid) < 1) {
			$msg =  JText::_('Select an item to delete');
			$mainframe->redirect('index.php?option='.$option.'&task=contents', $msg, 'error');
		}
		$id	=	implode(',',$cid);
		$query	=	'DELETE FROM `#__fcontent` '.
						'WHERE id IN ('.$id.')';
		$db->setQuery($query);
		
		$db->Query();

		$query	=	'DELETE FROM `#__fmedia` '.
						' WHERE cid IN ('.$id.')';
		$db->setQuery($query);

		$db->Query();				
		$query	=	'DELETE FROM `#__fcontent_receive` '.
						' WHERE cid IN ('.$id.')';
		$db->setQuery($query);
		
		$db->Query();
		return $msg;
	}
	function getContentEdit()
	{
		if ($this->_contentEdit) {
			return $this->_contentEdit;			
		}
		global $mainframe;
		// Initialize variables
		$doTask 	= JRequest::getCmd('task',null);
		
		$db			=& JFactory::getDBO();
		$user 		=& JFactory::getUser();
		$uid		= $user->get('id');
		$option		= JRequest::getCmd( 'option' );
		$cid		= JRequest::getVar( 'cid', array(0), '', 'array' );

		JArrayHelper::toInteger($cid, array(0));
		
		$edit		=	(strtolower($doTask) == 'content.edit') || ($cid[0]);
	
		$row =& JTable::getInstance('fcontent','Table');
	
		// load the row from the db table
		if ($edit)
			$row->load( $cid[0] );		
		
		$this->_feedEdit=$row;		
		return $this->_feedEdit;
	}
	function getListsEdit(){
		
		$db			=	&JFactory::getDBO();
		
		$cid		= JRequest::getVar( 'cid', array(0), '', 'array' );
	
		JArrayHelper::toInteger($cid, array(0));
		$doTask 	= JRequest::getCmd('task',null);
		$edit		=	strtolower($doTask) == 'link.edit';
		$sectionid	=	0;

		$row =$this->getContentEdit();
		// load the row from the db table
		if ($edit)
			$row->load( $cid[0] );		
		return 1;
	}
}